var _0 = Array();

var GPS_WEB_Point = function(x, y) {
	this.x = x;
	this.y = y;
};

var LabelOverlay = function(a) {
	this.latlng = a;// google.maps.LatLng
};
LabelOverlay.prototype.initialize = function(a) {
	this.map = a;
	var b = document.createElement("div");
	var c = a.getPane(G_MAP_FLOAT_PANE);
	c.appendChild(b);
	b.innerHTML = "";
	b.style.position = "absolute";
	b.style.overflow = 'visible';
	this.label = b;
	this.redraw();
};
LabelOverlay.prototype.redraw = function() {
	if (!this.map)
		return;
	var a = this.map.fromLatLngToDivPixel(this.latlng);
	if (!isNaN(a.x) && !isNaN(a.y)) {
		this.label.style.left = (a.x) + "px";
		this.label.style.top = (a.y - 8) + "px";
	}
	this.label.style.fontweight = "bold";
	if (this.map.getCurrentMapType() == G_NORMAL_MAP) {
		this.label.style.color = "black";
		// this.label.style.filter = "glow(color=white,strength=3)"
	} else {
		this.label.style.color = "white";
		// this.label.style.filter = "glow(color=black,strength=3)"
	}
};
LabelOverlay.prototype.remove = function() {
	this.label.parentNode.removeChild(this.label);
};
LabelOverlay.prototype.move = function(a) {
	this.latlng = a;
	this.redraw();
};
LabelOverlay.prototype.hideMe = function() {
	this.label.style.display = "none";
};
LabelOverlay.prototype.showMe = function() {
	this.label.style.display = "block";
};
LabelOverlay.prototype.put_text = function(a) {
	if (a != "") {
		a = "<li style='list-style:none;height:100%;overflow:visible;'>" + a
				+ "</li>";
	}
	this.label.innerHTML = a;
	this.redraw();
};

MyGoogleLabel.prototype = new google.maps.OverlayView();
function MyGoogleLabel(map, a/* GPS_LatLng */) {
	if (map) {
		this.setMap(map);
	}
	this.lat_ = a.lat();
	this.lng_ = a.lng();
}
MyGoogleLabel.prototype.div_ = null;
MyGoogleLabel.prototype.lat_ = 0;
MyGoogleLabel.prototype.lng_ = 0;
MyGoogleLabel.prototype.text_ = '';
MyGoogleLabel.prototype.onAdd = function() {
	// Note: an overlay's receipt of onAdd() indicates that
	// the map's panes are now available for attaching
	// the overlay to the map via the DOM.
	// Create the DIV and set some basic attributes.
	var div = document.createElement('DIV');
	div.style.borderStyle = "none";
	div.style.borderWidth = "0px";
	div.style.position = "absolute";
	// div.style.backgroundColor='white';
	div.style.whiteSpace = 'nowrap';

	div.innerHTML = this.text_;

	// Set the overlay's div_ property to this DIV
	this.div_ = div;

	// We add an overlay to a map via one of the map's panes.
	// We'll add this overlay to the overlayImage pane.
	var panes = this.getPanes();
	panes.overlayLayer.appendChild(div);
};

MyGoogleLabel.prototype.draw = function() {

	// Size and position the overlay. We use a southwest and northeast
	// position of the overlay to peg it to the correct position and size.
	// We need to retrieve the projection from this overlay to do this.
	var overlayProjection = this.getProjection();

	// Retrieve the southwest and northeast coordinates of this overlay
	// in latlngs and convert them to pixels coordinates.
	// We'll use these coordinates to resize the DIV.
	var point = overlayProjection.fromLatLngToDivPixel(new google.maps.LatLng(
			this.lat_, this.lng_));
	// Resize the image's DIV to fit the indicated dimensions.
	var div = this.div_;
	div.style.left = point.x + 'px';
	div.style.top = point.y + 'px';
};

MyGoogleLabel.prototype.onRemove = function() {
	this.div_.parentNode.removeChild(this.div_);
	this.div_ = null;
};

MyGoogleLabel.prototype.put_text = function(text) {
	if (this.div_) {
		this.div_.innerHTML = text;
	}
	this.text_ = text;
};

MyGoogleLabel.prototype.move = function(a /* google.maps.LatLng */) {
	var overlayProjection = this.getProjection();
	if (!overlayProjection || !this.div_) {
		return;
	}
	this.lat_ = a.lat();
	this.lng_ = a.lng();
	var point = overlayProjection.fromLatLngToDivPixel(a);

	// Resize the image's DIV to fit the indicated dimensions.
	var div = this.div_;
	div.style.left = point.x + 'px';
	div.style.top = point.y + 'px';
};

// Note that the visibility property must be a string enclosed in quotes
MyGoogleLabel.prototype.hide = function() {
	if (this.div_) {
		this.div_.style.display = "none";
	}
};

MyGoogleLabel.prototype.show = function() {
	if (this.div_) {
		this.div_.style.display = "block";
	}
};

var GPS_Label = function(a /* GPS_LatLng */, map) {
	this.text = "";
	this.point = {
		lat : 0,
		lng : 0
	};
	if (currentMap.mapProvider == 'geoserver') {
		return;
	}
	this.gm_label = new MyGoogleLabel(map, a);
};
GPS_Label.prototype.redraw = function() {
	if (this.geoLable) {

	} else
		this.gm_label.redraw();
};
GPS_Label.prototype.remove = function() {
	this.gm_label.setMap(null);
};
GPS_Label.prototype.move = function(a, offetX, offsetY) {
	this.point = {
		lng : a.lng(),
		lat : a.lat()
	};
	if (this.geoLable && this.gps_Map.mapProvider == 'geoserver') {
		var lonlat = new OpenLayers.LonLat(a.lng(), a.lat());
		var px = this.geoLable.map.getLayerPxFromLonLat(lonlat);
		if (offetX) {
			px.x = px.x + offetX;
		} else
			px.x = px.x + 15;
		if (offsetY) {
			px.y = px.y + offsetY;
		}
		this.geoLable.moveTo(px);
	} else
		this.gm_label.move(new google.maps.LatLng(a.lat(), a.lng()));
};
GPS_Label.prototype.hideMe = function() {
	if (this.geoLable) {
		this.geoLable.display(false);
	} else
		this.gm_label.hide();
};
GPS_Label.prototype.showMe = function() {
	if (this.geoLable) {
		this.geoLable.display(true);
	} else
		this.gm_label.show();
};
GPS_Label.prototype.put_text = function(a) {
	this.text = a;
	if (currentMap && currentMap.mapProvider == 'geoserver') {
		if (this.geoLable)
			this.geoLable
					.setTitle("<div style='background-color:yellow;white-space:nowrap'>"
							+ a + "</div>");
	} else
		this.gm_label
				.put_text("<div style='background-color:yellow;white-space:nowrap'>"
						+ a + "</div>");
};
// function GPSControl() {};
function GPSControl(map /* google.maps.Map */, country) {

	var _country = country;

	var controlDiv = document.createElement('DIV');

	// Set CSS styles for the DIV containing the control
	// Setting padding to 5 px will offset the control
	// from the edge of the map
	controlDiv.style.paddingTop = '5px';

	// Set CSS for the control border
	var controlUI = document.createElement('DIV');
	controlUI.style.backgroundColor = 'white';
	controlUI.style.borderStyle = 'solid';
	controlUI.style.borderWidth = '1px';
	controlUI.style.cursor = 'pointer';
	controlUI.style.textAlign = 'center';
	controlUI.title = 'Click to custom map';
	controlDiv.appendChild(controlUI);

	// Set CSS for the control interior
	var controlText = document.createElement('DIV');
	controlText.style.fontFamily = 'Arial,sans-serif';
	controlText.style.fontSize = '12px';
	controlText.innerHTML = '<b>Home</b>';
	controlUI.appendChild(controlText);
	this.container = controlText;
	var self = this;
	// Setup the click event listeners: simply set the map to
	google.maps.event.addDomListener(controlUI, 'click', function() {

		if (self.controlType == 'geomap') {
			if (currentMap.currentMapType == 'location') {
				// initializeLocationMap(geomapCountry);
				initializeLocationMap(_country);
				currentMap = location_map;
			} else if (currentMap.currentMapType == 'history') {
				// initHistoryMap(geomapCountry);
				initHistoryMap(_country);
				currentMap = history_map;
			} else if (currentMap.currentMapType == 'realtime') {
				// initializeRealtime(geomapCountry);
				initializeRealtime(_country);
				currentMap = g_map;
			}
		}
	});
	this.controlDiv = controlDiv;
}

GPSControl.prototype.put_content = function(a) {
	this.container.innerHTML = a;
};

GPSControl.prototype.controlType = 'geomap';

GPSControl.prototype.getDefaultPosition = function() {
	return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(60, 0));
};
var GPS_Control = function(a) {
	this.control = new GPSControl(a.map);
};
GPS_Control.prototype.put_content = function(a) {
	return this.control.put_content(a);
};
GPS_Control.prototype.getDefaultPosition = function() {
	return this.control.getDefaultPosition();
};
var GPS_LatLng = function(a, b) {
	this._1 = parseFloat(a);
	this._2 = parseFloat(b);
};
GPS_LatLng.prototype.lat = function() {
	return this._1;
};
GPS_LatLng.prototype.lng = function() {
	return this._2;
};
GPS_LatLng.prototype.setLat = function(l) {
	this._1 = l;
};
GPS_LatLng.prototype.setLng = function(l) {
	this._2 = l;
};
GPS_LatLng.prototype.distanceFrom = function(a) {
	var point = new OpenLayers.LonLat(this.lng(), this.lat());
	return point.distanceFrom(new OpenLayers.LonLat(a.lng(), a.lat()));
};
var GPS_Size = function(a, b) {
	this.width = a;
	this.height = b;
};
var GPS_Point = function(x, y) {
	this.x = x;
	this.y = y;
};
var GPS_Icon = function() {
};
var GPS_Map = function(id, g_default_lat, g_default_lng, g_default_level,
		g_default_type, mapProvider, country) {
	// id, g_default_lat, g_default_lng, g_default_level, g_default_type
	this.currentMapType = 'location';
	var f = null;
	this.mapLayer = document.getElementById(id);
	this.mapProvider = mapProvider;
	if (mapProvider == 'geoserver') {
		this.geoMarkers = new OpenLayers.Layer.Markers("Markers");

		if (country == 'malaysia') { // level越大地图越详细
			f = initMalaysiaMap(id, geo_default_lat, geo_default_lng, 9, this);
		} else if (country == 'nigeria') { // level越大地图越详细
			f = initNegeriaMap(id, geo_default_lat, geo_default_lng, 9, this);
		} else if (country == 'kenya') { // level越大地图越详细
			f = initKenyaMap(id, geo_default_lat, geo_default_lng, 9, this);
		} else if (country == 'tanzania') { // level越大地图越详细
			f = initTanzaniaMap(id, geo_default_lat, geo_default_lng, 9, this);
		}
		f.addLayer(this.geoMarkers);
		this.geoMarkers.setZIndex(350);
	} else { // google map
		var myOptions = {
			zoom : g_default_level,
			ceneter : new google.maps.LatLng(g_default_lat, g_default_lng),
			mapTypeId : google.maps.MapTypeId.ROADMAP
		};

		f = new google.maps.Map(this.mapLayer, myOptions);

		if (geomapCountry == 'malaysia') {
			var control = new GPSControl(f, 'malaysia');
			// f.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT,
			// new GSize(190, 0)));
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Malaysia Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
		} else if (geomapCountry == 'nigeria') {
			var control = new GPSControl(f, 'nigeria');
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Nigeria Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
		} else if (geomapCountry == 'china') {
			control = new GPSControl(f, 'malaysia');
			// f.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT,
			// new GSize(190, 0)));
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Malaysia Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
			control = new GPSControl(f, 'nigeria');
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Nigeria Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);

			control = new GPSControl(f, 'kenya');
			// f.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT,
			// new GSize(190, 0)));
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Kenya & Uganda Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
			control = new GPSControl(f, 'tanzania');
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Tanzania Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
		} else if (geomapCountry == 'kenya' || geomapCountry == 'tanzania') { // Kenya
																				// Ugunda
																				// Tanzania
			control = new GPSControl(f, 'kenya');
			// f.addControl(control, new GControlPosition(G_ANCHOR_TOP_RIGHT,
			// new GSize(190, 0)));
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Kenya & Uganda Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
			control = new GPSControl(f, 'tanzania');
			control
					.put_content('<div style="background-color: white; border: 1px solid black; text-align: center; width: 120px; right: 10.2em; cursor: pointer;background-color:#FFFFFF;">Tanzania Map</div>');
			f.controls[google.maps.ControlPosition.TOP_RIGHT]
					.push(control.controlDiv);
		}

		// f.addControl(mapControl, new GControlPosition(G_ANCHOR_TOP_RIGHT, new
		// GSize(0, 0)));
		f.setCenter(new google.maps.LatLng(g_default_lat, g_default_lng));
	}

	this.markers = Array();
	this.address_labels = Array();
	this.map = f;
	_0.push(this);
};
var geoserverMapHost = "http://" + window.location.hostname + ":8019";
function initMalaysiaMap(id, g_default_lat, g_default_lng, g_default_level,
		gpsMap) {
	// pink tile avoidance
	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 25.4 / 0.28;
	var format = 'image/png';

	// style the sketch fancy
	var sketchSymbolizers = {
		"Point" : {
			pointRadius : 4,
			graphicName : "square",
			fillColor : "white",
			fillOpacity : 1,
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#333333"
		},
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			strokeDashstyle : "solid"
		},
		"Polygon" : {
			strokeWidth : 2,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			fillColor : "#00FF00",
			fillOpacity : 0.35
		}
	};

	var style = new OpenLayers.Style();
	style.addRules([ new OpenLayers.Rule({
		symbolizer : sketchSymbolizers
	}) ]);
	var styleMap = new OpenLayers.StyleMap({
		"default" : style
	});
	var delay = 1;
	if (Ext.isIE) {
		delay = 150;
	}

	var measureControls = {
		line : new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
			persist : true,
			partialDelay : delay,
			// displaySystem:'english',
			// displaySystemUnits:[{metric: ['km']}],
			handlerOptions : {
				layerOptions : {
					styleMap : styleMap
				}
			}

		})
	};

	var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
	gpsMap.measureControls = measureControls;
	gpsMap.geoLineLayerRef = new geoLineLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonLayerRef = new geoPolygonLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonControlRef = new OpenLayers.Control.DrawFeature(
			polygonLayer, OpenLayers.Handler.Polygon, {
				callbacks : {
					create : function() {
						geoCreated(arguments);
					},
					modify : function() {
						geoModify(arguments);
					},
					done : function() {
						geoDone(arguments);
					}
				},
				handlerOptions : {
					layerOptions : {
						styleMap : styleMap
					}
				}
			});

	var bounds = new OpenLayers.Bounds(99.612, 0.854, 119.926,
			7.6253333333333355);
	var options = {
		controls : [],
		resolutions : [ 0.02645052083333334, 0.01322526041666667,
				0.006612630208333335, 0.0033063151041666677,
				0.0016531575520833338, 8.265787760416669E-4,
				4.1328938802083346E-4, 2.0664469401041673E-4,
				1.0332234700520837E-4, 5.166117350260418E-5,
				2.583058675130209E-5, 1.2915293375651046E-5,
				6.457646687825523E-6, 3.2288233439127614E-6 ],
		maxExtent : bounds,
		// maxResolution: 0.1,
		projection : "EPSG:3376",
		units : "meters",
		allOverlays : true
	};

	document.getElementById(id).innerHTML = "";
	var map = new OpenLayers.Map(id, options);

	// for(var key in controls) {
	// map.addControl(controls[key]);
	// }
	map.addControl(measureControls.line);
	map.addLayer(gpsMap.geoLineLayerRef.layer);
	map.addLayer(gpsMap.geoPolygonLayerRef.layer);
	map.addControl(gpsMap.geoPolygonControlRef);

	// setup tiled layer
	tiled = new OpenLayers.Layer.WMS("cite:malaysia_polyline", geoserverMapHost
			+ "/geoserver/gwc/service/wms", {
		width : '800',
		layers : 'malaysia',
		styles : '',
		srs : 'EPSG:3376',
		height : '600',
		format : format,
		isBaseLayer : true,
		tiled : 'true',
		TRANSPARENT : true
	}, {
		tileSize : new OpenLayers.Size(256, 256)

	});

	// setup single tiled layer
	untiled = new OpenLayers.Layer.WMS(
			"malaysia_region",
			geoserverMapHost + "/geoserver/wms",
			{
				width : '800',
				layers : 'cite:malaysia_polyline,cite:malaysia_point,cite:malaysia_region',
				styles : '',
				srs : 'EPSG:3376',
				tiled : 'true',
				height : '600',
				format : format
			}, {
				singleTile : true,
				ratio : 1
			});

	map.addLayers([ tiled ]);

	// build up all controls alert
	map.addControl(new OpenLayers.Control.PanZoomBar({
		position : new OpenLayers.Pixel(2, 15)
	}));
	map.addControl(new OpenLayers.Control.Navigation());
	// map.addControl(new OpenLayers.Control.Scale($('scale')));
	// map.addControl(new OpenLayers.Control.MousePosition({element:
	// $('location')}));

	// var control = new OpenLayers.Control({title:'Google Map', position: new
	// OpenLayers.Pixel(15, 20)});
	var mapSize = map.getSize();
	var button = new OpenLayers.Control.CustomButton(
			{
				content : '<div onclick="changeToGoogleMap();"'
						+ ' style="background-color: white; border: 1px solid black; text-align: center; width: 120px; float:right; cursor: pointer;background-color:#FFFFFF;">Google Map</div>',
				position : new OpenLayers.Pixel(mapSize.w - 190, 4),
				displayClass : 'openLayerButtonCss'
			});
	map.addControl(button);
	map.googleMapButton = button;

	// button.events.register("click", button, function(){alert(99);});

	map.zoomToExtent(bounds);

	// support GetFeatureInfo
	map.events.register('click', map, function(e) {

		var lonlat = map.getLonLatFromViewPortPx(e.xy);
		// console.log("map click",e);
	});
	var lonlat = new OpenLayers.LonLat(g_default_lng, g_default_lat);
	map.setCenter(lonlat, g_default_level);

	return map;
}

function initNegeriaMap(id, g_default_lat, g_default_lng, g_default_level,
		gpsMap) {
	// pink tile avoidance
	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 90.71428571428572;
	var format = 'image/png8';

	// style the sketch fancy
	var sketchSymbolizers = {
		"Point" : {
			pointRadius : 4,
			graphicName : "square",
			fillColor : "white",
			fillOpacity : 1,
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#333333"
		},
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			strokeDashstyle : "solid"
		},
		"Polygon" : {
			strokeWidth : 2,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			fillColor : "#00FF00",
			fillOpacity : 0.35
		}
	};

	var style = new OpenLayers.Style();
	style.addRules([ new OpenLayers.Rule({
		symbolizer : sketchSymbolizers
	}) ]);
	var styleMap = new OpenLayers.StyleMap({
		"default" : style
	});
	var delay = 1;
	if (Ext.isIE) {
		delay = 150;
	}

	var measureControls = {
		line : new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
			persist : true,
			partialDelay : delay,
			// displaySystem:'english',
			// displaySystemUnits:[{metric: ['km']}],
			handlerOptions : {
				layerOptions : {
					styleMap : styleMap
				}
			}

		})
	};

	var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
	gpsMap.measureControls = measureControls;
	gpsMap.geoLineLayerRef = new geoLineLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonLayerRef = new geoPolygonLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonControlRef = new OpenLayers.Control.DrawFeature(
			polygonLayer, OpenLayers.Handler.Polygon, {
				callbacks : {
					create : function() {
						geoCreated(arguments);
					},
					modify : function() {
						geoModify(arguments);
					},
					done : function() {
						geoDone(arguments);
					}
				},
				handlerOptions : {
					layerOptions : {
						styleMap : styleMap
					}
				}
			});

	var bounds = new OpenLayers.Bounds(2.685, 4.287, 14.676, 16.278);
	var options = {
		controls : [],
		resolutions : [ 0.04683984375, 0.023419921875, 0.0117099609375,
				0.00585498046875, 0.002927490234375, 0.0014637451171875,
				7.3187255859375E-4, 3.65936279296875E-4, 1.829681396484375E-4,
				9.148406982421875E-5, 4.5742034912109374E-5,
				2.2871017456054687E-5, 1.1435508728027343E-5,
				5.717754364013672E-6, 2.858877182006836E-6,
				1.429438591003418E-6 ],
		maxExtent : bounds,
		// maxResolution: 0.1,
		projection : "EPSG:26392",
		units : "meters",
		allOverlays : true
	};

	document.getElementById(id).innerHTML = "";
	var map = new OpenLayers.Map(id, options);

	// for(var key in controls) {
	// map.addControl(controls[key]);
	// }
	map.addControl(measureControls.line);
	map.addLayer(gpsMap.geoLineLayerRef.layer);
	map.addLayer(gpsMap.geoPolygonLayerRef.layer);
	map.addControl(gpsMap.geoPolygonControlRef);

	// setup tiled layer
	tiled = new OpenLayers.Layer.WMS("Nigeria", geoserverMapHost
			+ "/geoserver/gwc/service/wms", {
		width : '800',
		layers : 'negeria',
		styles : '',
		srs : 'EPSG:26392',
		height : '600',
		format : format,
		isBaseLayer : true,
		tiled : 'true',
		TRANSPARENT : true
	}, {
		tileSize : new OpenLayers.Size(256, 256)

	});

	tiled.setZIndex(100);
	map.addLayers([ tiled ]);
	tiled.setZIndex(100);

	// build up all controls alert
	map.addControl(new OpenLayers.Control.PanZoomBar({
		position : new OpenLayers.Pixel(2, 15)
	}));
	map.addControl(new OpenLayers.Control.Navigation());
	// map.addControl(new OpenLayers.Control.Scale($('scale')));
	// map.addControl(new OpenLayers.Control.MousePosition({element:
	// $('location')}));

	// var control = new OpenLayers.Control({title:'Google Map', position: new
	// OpenLayers.Pixel(15, 20)});
	var mapSize = map.getSize();
	var button = new OpenLayers.Control.CustomButton(
			{
				content : '<div onclick="changeToGoogleMap();"'
						+ ' style="background-color: white; border: 1px solid black; text-align: center; width: 120px; float:right; cursor: pointer;background-color:#FFFFFF;">Google Map</div>',
				position : new OpenLayers.Pixel(mapSize.w - 190, 4),
				displayClass : 'openLayerButtonCss'
			});
	map.addControl(button);
	map.googleMapButton = button;

	// button.events.register("click", button, function(){alert(99);});

	map.zoomToExtent(bounds);

	// support GetFeatureInfo
	map.events.register('click', map, function(e) {

		var lonlat = map.getLonLatFromViewPortPx(e.xy);
		// console.log("map click",e);
	});
	var lonlat = new OpenLayers.LonLat(g_default_lng, g_default_lat);
	map.setCenter(lonlat, g_default_level);

	return map;
}

function initKenyaMap(id, g_default_lat, g_default_lng, g_default_level, gpsMap) {

	// pink tile avoidance
	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 90.71428571428572;
	var format = 'image/png';

	// style the sketch fancy
	var sketchSymbolizers = {
		"Point" : {
			pointRadius : 4,
			graphicName : "square",
			fillColor : "white",
			fillOpacity : 1,
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#333333"
		},
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			strokeDashstyle : "solid"
		},
		"Polygon" : {
			strokeWidth : 2,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			fillColor : "#00FF00",
			fillOpacity : 0.35
		}
	};

	var style = new OpenLayers.Style();
	style.addRules([ new OpenLayers.Rule({
		symbolizer : sketchSymbolizers
	}) ]);
	var styleMap = new OpenLayers.StyleMap({
		"default" : style
	});
	var delay = 1;
	if (Ext.isIE) {
		delay = 150;
	}

	var measureControls = {
		line : new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
			persist : true,
			partialDelay : delay,
			// displaySystem:'english',
			// displaySystemUnits:[{metric: ['km']}],
			handlerOptions : {
				layerOptions : {
					styleMap : styleMap
				}
			}

		})
	};

	var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
	gpsMap.measureControls = measureControls;
	gpsMap.geoLineLayerRef = new geoLineLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonLayerRef = new geoPolygonLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonControlRef = new OpenLayers.Control.DrawFeature(
			polygonLayer, OpenLayers.Handler.Polygon, {
				callbacks : {
					create : function() {
						geoCreated(arguments);
					},
					modify : function() {
						geoModify(arguments);
					},
					done : function() {
						geoDone(arguments);
					}
				},
				handlerOptions : {
					layerOptions : {
						styleMap : styleMap
					}
				}
			});

	var bounds = new OpenLayers.Bounds(29.583, -11.106, 44.857,
			4.168000000000001);
	var options = {
		resolutions : [ 0.059664062500000004, 0.029832031250000002,
				0.014916015625000001, 0.0074580078125000004,
				0.0037290039062500002, 0.0018645019531250001,
				9.322509765625001E-4, 4.6612548828125003E-4,
				2.3306274414062501E-4, 1.1653137207031251E-4,
				5.8265686035156253E-5, 2.9132843017578127E-5,
				1.4566421508789063E-5, 7.283210754394532E-6 ],
		projection : new OpenLayers.Projection('EPSG:22994'),
		maxExtent : bounds,
		units : "meters",
		controls : [],
		allOverlays : true
	};

	document.getElementById(id).innerHTML = "";
	var map = new OpenLayers.Map(id, options);

	// for(var key in controls) {
	// map.addControl(controls[key]);
	// }
	map.addControl(measureControls.line);
	map.addLayer(gpsMap.geoLineLayerRef.layer);
	map.addLayer(gpsMap.geoPolygonLayerRef.layer);
	map.addControl(gpsMap.geoPolygonControlRef);

	// setup tiled layer
	tiled = new OpenLayers.Layer.WMS("Kenya", geoserverMapHost
			+ "/geoserver/gwc/service/wms", {
		width : '800',
		layers : 'kenya',
		styles : '',
		srs : 'EPSG:22994',
		height : '600',
		format : format,
		isBaseLayer : true,
		tiled : 'true',
		TRANSPARENT : true
	}, {
		tileSize : new OpenLayers.Size(256, 256)

	});

	tiled.setZIndex(100);
	map.addLayers([ tiled ]);
	tiled.setZIndex(100);

	// build up all controls alert
	map.addControl(new OpenLayers.Control.PanZoomBar({
		position : new OpenLayers.Pixel(2, 15)
	}));
	map.addControl(new OpenLayers.Control.Navigation());
	// map.addControl(new OpenLayers.Control.Scale($('scale')));
	// map.addControl(new OpenLayers.Control.MousePosition({element:
	// $('location')}));

	// var control = new OpenLayers.Control({title:'Google Map', position: new
	// OpenLayers.Pixel(15, 20)});
	var mapSize = map.getSize();
	var button = new OpenLayers.Control.CustomButton(
			{
				content : '<div onclick="changeToGoogleMap();"'
						+ ' style="background-color: white; border: 1px solid black; text-align: center; width: 120px; float:right; cursor: pointer;background-color:#FFFFFF;">Google Map</div>',
				position : new OpenLayers.Pixel(mapSize.w - 190, 4),
				displayClass : 'openLayerButtonCss'
			});
	map.addControl(button);
	map.googleMapButton = button;

	// button.events.register("click", button, function(){alert(99);});

	map.zoomToExtent(bounds);

	// support GetFeatureInfo
	map.events.register('click', map, function(e) {

		var lonlat = map.getLonLatFromViewPortPx(e.xy);
		// console.log("map click",e);
	});
	var lonlat = new OpenLayers.LonLat(g_default_lng, g_default_lat);
	map.setCenter(lonlat, g_default_level);

	return map;

}

function initTanzaniaMap(id, g_default_lat, g_default_lng, g_default_level,
		gpsMap) {
	// pink tile avoidance
	OpenLayers.IMAGE_RELOAD_ATTEMPTS = 5;
	// make OL compute scale according to WMS spec
	OpenLayers.DOTS_PER_INCH = 90.71428571428572;
	var format = 'image/png';

	// style the sketch fancy
	var sketchSymbolizers = {
		"Point" : {
			pointRadius : 4,
			graphicName : "square",
			fillColor : "white",
			fillOpacity : 1,
			strokeWidth : 1,
			strokeOpacity : 1,
			strokeColor : "#333333"
		},
		"Line" : {
			strokeWidth : 3,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			strokeDashstyle : "solid"
		},
		"Polygon" : {
			strokeWidth : 2,
			strokeOpacity : 1,
			strokeColor : "#FF0000",
			fillColor : "#00FF00",
			fillOpacity : 0.35
		}
	};

	var style = new OpenLayers.Style();
	style.addRules([ new OpenLayers.Rule({
		symbolizer : sketchSymbolizers
	}) ]);
	var styleMap = new OpenLayers.StyleMap({
		"default" : style
	});
	var delay = 1;
	if (Ext.isIE) {
		delay = 150;
	}

	var measureControls = {
		line : new OpenLayers.Control.Measure(OpenLayers.Handler.Path, {
			persist : true,
			partialDelay : delay,
			// displaySystem:'english',
			// displaySystemUnits:[{metric: ['km']}],
			handlerOptions : {
				layerOptions : {
					styleMap : styleMap
				}
			}

		})
	};

	var polygonLayer = new OpenLayers.Layer.Vector("Polygon Layer");
	gpsMap.measureControls = measureControls;
	gpsMap.geoLineLayerRef = new geoLineLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonLayerRef = new geoPolygonLayer({
		layerOptions : {
			styleMap : styleMap
		}
	});
	gpsMap.geoPolygonControlRef = new OpenLayers.Control.DrawFeature(
			polygonLayer, OpenLayers.Handler.Polygon, {
				callbacks : {
					create : function() {
						geoCreated(arguments);
					},
					modify : function() {
						geoModify(arguments);
					},
					done : function() {
						geoDone(arguments);
					}
				},
				handlerOptions : {
					layerOptions : {
						styleMap : styleMap
					}
				}
			});

	var bounds = new OpenLayers.Bounds(29.583, -11.106, 44.857,
			4.168000000000001);
	var options = {
		resolutions : [ 0.059664062500000004, 0.029832031250000002,
				0.014916015625000001, 0.0074580078125000004,
				0.0037290039062500002, 0.0018645019531250001,
				9.322509765625001E-4, 4.6612548828125003E-4,
				2.3306274414062501E-4, 1.1653137207031251E-4,
				5.8265686035156253E-5, 2.9132843017578127E-5,
				1.4566421508789063E-5, 7.283210754394532E-6 ],
		projection : new OpenLayers.Projection('EPSG:22994'),
		maxExtent : bounds,
		units : "meters",
		controls : [],
		allOverlays : true
	};

	document.getElementById(id).innerHTML = "";
	var map = new OpenLayers.Map(id, options);

	// for(var key in controls) {
	// map.addControl(controls[key]);
	// }
	map.addControl(measureControls.line);
	map.addLayer(gpsMap.geoLineLayerRef.layer);
	map.addLayer(gpsMap.geoPolygonLayerRef.layer);
	map.addControl(gpsMap.geoPolygonControlRef);

	// setup tiled layer
	tiled = new OpenLayers.Layer.WMS("Tanzania", geoserverMapHost
			+ "/geoserver/gwc/service/wms", {
		width : '800',
		layers : 'tanzania',
		styles : '',
		srs : 'EPSG:22994',
		height : '600',
		format : format,
		isBaseLayer : true,
		tiled : 'true',
		TRANSPARENT : true
	}, {
		tileSize : new OpenLayers.Size(256, 256)

	});

	tiled.setZIndex(100);
	map.addLayers([ tiled ]);
	tiled.setZIndex(100);

	// build up all controls alert
	map.addControl(new OpenLayers.Control.PanZoomBar({
		position : new OpenLayers.Pixel(2, 15)
	}));
	map.addControl(new OpenLayers.Control.Navigation());
	// map.addControl(new OpenLayers.Control.Scale($('scale')));
	// map.addControl(new OpenLayers.Control.MousePosition({element:
	// $('location')}));

	// var control = new OpenLayers.Control({title:'Google Map', position: new
	// OpenLayers.Pixel(15, 20)});
	var mapSize = map.getSize();
	var button = new OpenLayers.Control.CustomButton(
			{
				content : '<div onclick="changeToGoogleMap();"'
						+ ' style="background-color: white; border: 1px solid black; text-align: center; width: 120px; float:right; cursor: pointer;background-color:#FFFFFF;">Google Map</div>',
				position : new OpenLayers.Pixel(mapSize.w - 190, 4),
				displayClass : 'openLayerButtonCss'
			});
	map.addControl(button);
	map.googleMapButton = button;

	// button.events.register("click", button, function(){alert(99);});

	map.zoomToExtent(bounds);

	// support GetFeatureInfo
	map.events.register('click', map, function(e) {

		var lonlat = map.getLonLatFromViewPortPx(e.xy);
		// console.log("map click",e);
	});
	var lonlat = new OpenLayers.LonLat(g_default_lng, g_default_lat);
	map.setCenter(lonlat, g_default_level);

	return map;
}

function changeToGoogleMap() {
	if (currentMap.currentMapType == 'location') {
		initializeLocationMap();
		currentMap = location_map;
	} else if (currentMap.currentMapType == 'history') {
		initHistoryMap();
		currentMap = history_map;
	} else if (currentMap.currentMapType == 'realtime') {
		initializeRealtime();
		currentMap = g_map;
	}
}

var previousPoint = null;
function handleMeasurements(event) {
	var geometry = event.geometry;
	var units = event.units;
	var order = event.order;
	var measure = event.measure;
	// console.log(order, geometry.components, geometry.components.length);
	var cur = geometry.components[geometry.components.length - 1];
	// console.log('before point',cur);
	if (previousPoint != null
			&& (Math.abs(cur.x - previousPoint.x) < 0.0001 && Math.abs(cur.y
					- previousPoint.y) < 0.0001)) {
		// console.log(geometry.components,'=='+cur.x, previousPoint.x,
		// Math.abs(cur.x - previousPoint.x), Math.abs(cur.y -
		// previousPoint.y));
		return;
	}
	// console.log('set point',cur);
	previousPoint = {
		x : cur.x,
		y : cur.y
	};
	var curPoint = new OpenLayers.LonLat(cur.x, cur.y);
	var prevPoint = null;
	var prev = geometry.components[geometry.components.length - 3];
	var curPoint = new OpenLayers.LonLat(cur.x, cur.y);
	var distance = 0;
	if (prev) {
		// console.log(geometry.components);
		var points = [];
		for ( var i = 0; i < geometry.components.length - 1; i++) {
			var point = geometry.components[i];
			var has = false;
			for ( var j = 0; j < points.length; j++) {
				if (Math.abs(points[j].x - point.x) < 0.0001
						&& Math.abs(points[j].y - point.y) < 0.0001) {
					has = true;
				}
			}
			if (!has) {
				points.push(point);
			}
		}
		var curPoint2 = null;

		for ( var i = 0; i < points.length; i++) {
			var point = points[i];
			curPoint2 = new OpenLayers.LonLat(point.x, point.y);
			if (prevPoint) {
				distance = distance + curPoint2.distanceFrom(prevPoint);
				// console.log("1111111="+curPoint2.distanceFrom(prevPoint)/1000,'
				// ii='+i);
			}
			prevPoint = curPoint2;
		}

		var size = new OpenLayers.Size(20, 33);
		var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
		var icon = new OpenLayers.Icon('openLayers/customer/point_icon.png',
				size, offset);
		var marker1 = new OpenLayers.MarkerLabel(curPoint, {
			w : 13,
			h : 12
		}, "<div style='background-color:white;'>" + distance.toFixed(2)
				+ "KM</div>");
		marker1.markerType = 'measureLabel';
		currentMap.geoMarkers.addMarker(marker1);
	}
}

function handleEndMeasurements(event) {
	var geometry = event.geometry;
	var units = event.units;
	var order = event.order;
	var measure = event.measure;
	var distance = 0;
	// console.log(geometry, "units="+units, "order="+order,
	// "measure="+measure);
	// console.log('ttttttttttttttttttt',order, geometry.components,
	// geometry.components.length);
	var prevPoint = null;

	for ( var i = 0; i < geometry.components.length; i++) {
		var point = geometry.components[i];
		var curPoint = new OpenLayers.LonLat(point.x, point.y);
		if (prevPoint) {
			distance = distance + curPoint.distanceFrom(prevPoint);
			// console.log("i="+i, i, curPoint, prevPoint,
			// curPoint.distanceFrom(prevPoint)/1000);
		}
		prevPoint = curPoint;
	}

	var point = geometry.components[geometry.components.length - 1];
	var lonlat = new OpenLayers.LonLat(point.x, point.y);
	var size = new OpenLayers.Size(13, 12);
	var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
	var icon = new OpenLayers.Icon('images/clear.jpg', size);
	var marker1 = new OpenLayers.Marker(lonlat, icon);
	var marker1 = new OpenLayers.MarkerLabel(lonlat, {
		w : 13,
		h : 12
	}, "<div style='background-color:white;'>" + distance.toFixed(2)
			+ "KM</div>");
	marker1.markerType = 'measureLabel';
	currentMap.geoMarkers.addMarker(marker1);
	var marker2 = new OpenLayers.Marker(lonlat, icon.clone());
	marker2.markerType = 'measureLabel';
	currentMap.geoMarkers.addMarker(marker2);
	marker2.events.on({
		"click" : function() {
			currentMap.measureControls.line.deactivate();
			currentMap.geoMarkers.removeMarker(marker2);
			currentMap.map.div.style.cursor = '';
			var removes = [];
			for ( var i = 0; i < currentMap.geoMarkers.markers.length; i++) {
				var m = currentMap.geoMarkers.markers[i];
				if (m.markerType == 'measureLabel') {
					// currentMap.geoMarkers.removeMarker(m);
					removes.push(m);
					// m.erase();
				}
			}
			for ( var i = 0; i < removes.length; i++) {
				var m = removes[i];
				currentMap.geoMarkers.removeMarker(m);
			}
		},
		'mouseover' : function() {
			currentMap.map.div.style.cursor = 'pointer';
		},
		'mouseout' : function() {
			currentMap.map.div.style.cursor = '';
		}
	});

}

// 设置地图缩放级别
GPS_Map.prototype.setZoom = function(level) {
	if (this.mapProvider == 'geoserver') {
		this.map.zoomTo(level);
	} else {
		this.map.setZoom(level);
	}
};

GPS_Map.prototype.addListener = function(d, e) {
	var f = this;

	if (this.mapProvider == 'geoserver') {

		if (d == 'drag') {

			Ext.get(this.map.div).addListener('mousedown', function() {
				f.mousedown = true;
			});
			Ext.get(this.map.div).addListener('mouseup', function() {
				f.mousedown = false;
			});
			var handler = function() {
				if (f.mousedown) {
					e(f, arguments);
				}
			};
			this.map.events.on({
				"mousemove" : handler
			});
			return handler;

		} else {
			var handler = function(evt) {

				var lonlat = f.map.getLonLatFromViewPortPx(evt.xy);
				if (lonlat)
					e(f, new GPS_LatLng(lonlat.lat, lonlat.lon));
				else
					e(f, new GPS_LatLng(0, 0));
			};
			this.map.events.register(d + '', this.map, handler);
			return handler;
		}

		return;
	}

	if (d == "click") {
		return google.maps.event.addListener(this.map, d, function(a, b) {
			if (a != null) {
				return e(this, new GPS_LatLng(a.latLng.lat(), a.latLng.lng()));
			} else {
				return e(this, new GPS_LatLng(0, 0));
			}
		});
	} else if (d == "mousemove") {
		return google.maps.event.addListener(this.map, d, function(a) {
			return e(this, new GPS_LatLng(a.latLng.lat(), a.latLng.lng()));
		});
	}
	return google.maps.event.addListener(this.map, d, function() {
		return e(f, arguments);
	});
};

GPS_Map.prototype.fromDivPixelToLatLng = function(point) {
	return this.map.fromDivPixelToLatLng(point);
};

GPS_Map.prototype.removeListener = function(a, i) {
	if (this.mapProvider == 'geoserver') {
		this.map.events.unregister(a, null, i);
	} else
		return google.maps.event.removeListener(i);
};


var points=new Array();
var pointspre =null;
GPS_Map.prototype.panTo = function(a/* GPS_LatLng */) {
	if (this.mapProvider == 'geoserver') {
		var lonlat = new OpenLayers.LonLat(a.lng(), a.lat());
		return this.map.panTo(lonlat);
	} else
    var points = new google.maps.LatLng(a.lat(), a.lng());
    if(pointspre==null)
    {
    pointspre=points;
    }else
    {
     points[0]=pointspre;
     points[1]=points;
    this.midArrows(points);
    pointspre=points;
    }
	return this.map.panTo(points);
};

// 多个箭头
GPS_Map.prototype.midArrows = function(points) {
	if (points.length < 2) {
		return;
	}	  

	 for (var i=1; i < points.length; i++)
     { 
         var p1=points[i-1];
         var p2=points[i];
         var dir = bearing(p1,p2);
         var dir = Math.round(dir/3) * 3;
         while (dir >= 120) {dir -= 120;}
        arrowIcon = new google.maps.MarkerImage('http://www.google.com/intl/en_ALL/mapfiles/dir_'+dir+'.png',new google.maps.Size(24, 24),new google.maps.Point(0,0),new google.maps.Point(12, 12));
        
        var marker = new google.maps.Marker({position: points[i],map: map,icon: arrowIcon}); 
     }
};

 var degreesPerRadian = 180.0/Math.PI;
        function bearing(from,to)
        {
            var lat1 = from.lat() * Math.PI/180;
            var lon1 = from.lng() * Math.PI/180;
            var lat2 = to.lat() * Math.PI/180;
            var lon2 = to.lng() * Math.PI/180;
            var angle = - Math.atan2( Math.sin( lon1 - lon2 ) * Math.cos( lat2 ), Math.cos( lat1 ) * Math.sin( lat2 ) - Math.sin( lat1 ) * Math.cos( lat2 ) * Math.cos( lon1 - lon2 ));
            if (angle < 0.0 )
                angle += Math.PI * 2.0;
            angle = angle * degreesPerRadian;
            angle = angle.toFixed(1);
            return angle;
        };
        
// 获取中心位置
GPS_Map.prototype.getCenter = function() { // GPS_LatLng
	if (this.mapProvider == 'geoserver') {
		var lonlat = this.map.getCenter();
		var p = new GPS_LatLng(lonlat.lat, lonlat.lon);
		return p;
	} else {
		var center = this.map.getCenter();
		var p = new GPS_LatLng(center.lat(), center.lng());
		return p;
	}

	return this.centerPoint;
};

/**
 * 获取网页坐标
 * 
 * @param p
 *            GPS_LatLng
 * @return
 */
GPS_Map.prototype.getWebPos = function(p /* GPS_LatLng */) { //  
	if (this.mapProvider == 'geoserver') {
		return null; // 没有实现
	} else {
		p = new google.maps.LatLng(p.lat(), p.lng());
		var webPoint = this.map.getProjection().fromLatLngToPoint(p); // 不是pixel
																		// google.maps.Point
		return new GPS_WEB_Point(webPoint.x, webPoint.y);
	}

	return null;
};

GPS_Map.prototype.addOverlay = function(a) {
	this.markers.push(a);
	a.map = this.map;
	if (this.mapProvider == 'geoserver') {
		var size = new OpenLayers.Size(20, 33);
		var offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
		var icon = null;// new
						// OpenLayers.Icon('http://boston.openguides.org/markers/AQUA.png',size,offset);
		if (a.image) {
			size = new OpenLayers.Size(a.width, a.height);
			offset = new OpenLayers.Pixel(-(size.w / 2), -size.h);
			icon = new OpenLayers.Icon(a.image, size, offset);
		} else {
			icon = new OpenLayers.Icon('openLayers/customer/point_icon.png',
					size, offset);
		}
		// if(a.title)
		// a.geoMarker = new OpenLayers.MarkerLabel(new OpenLayers.LonLat(a.lng,
		// a.lat),icon.clone(), a.title);
		// else
		a.geoMarker = new OpenLayers.Marker(
				new OpenLayers.LonLat(a.lng, a.lat), icon);
		this.geoMarkers.addMarker(a.geoMarker);

	} else {
		if (!this.googleMarkers)
			this.googleMarkers = [];
		this.googleMarkers.push(a.marker);
		return a.marker.setMap(this.map);
	}
};

GPS_Map.prototype.checkResize = function(w, h) {
	if (this.mapProvider == 'geoserver') {
		var size = this.map.getSize();
		this.map.googleMapButton.moveTo(new OpenLayers.Pixel(size.w - 190, 4));
		this.map.updateSize();
	} else
		google.maps.event.trigger(this.map, 'resize');
	// this.map.checkResize();
};

GPS_Map.prototype.removeOverlay = function(a) {
	if (this.mapProvider == 'geoserver') {
		this.geoMarkers.removeMarker(a.geoMarker);
	} else
		return a.marker.setMap(null);
};
GPS_Map.prototype.addLabel = function(a) {
	if (this.mapProvider == 'geoserver') {
		var lonlat = new OpenLayers.LonLat(a.point.lng, a.point.lat);
		var marker1 = new OpenLayers.MarkerLabel(lonlat, {
			w : -10,
			h : 12
		}, "<div style='background-color:white;'>" + a.text + "</div>");
		this.geoMarkers.addMarker(marker1);
		a.geoLable = marker1;
		a.gps_Map = this;
	} else {
		a.gm_label.lat = this.lat;
		a.gm_label.lng = this.lng;
		var r = a.gm_label.setMap(this.map);
		a.gm_label.move(new google.maps.LatLng(this.lat, this.lng));
		return r;
	}
};
GPS_Map.prototype.addPolyline = function(a) {
	if (this.mapProvider == 'geoserver') {
		this.geoLineLayerRef.addLine(a.geoLine);
	} else {
		if (!this.googleLines)
			this.googleLines = [];
		this.googleLines.push(a.polyline);
		return a.polyline.setMap(this.map);
	}
};
GPS_Map.prototype.removePolyline = function(a) {
	if (this.mapProvider == 'geoserver') {
		this.geoLineLayerRef.removeLine(a.geoLine);
	} else
		return a.polyline.setMap(null);
};

GPS_Map.prototype.addPolygon = function(a/* GPS_Polygon */) {
	if (this.mapProvider == 'geoserver') {
		this.geoPolygonLayerRef.addPolygon(a);
	} else {
		return a.polygon.setMap(this.map);
	}
};

GPS_Map.prototype.removePolygon = function(a/* GPS_Polygon */) {
	if (this.mapProvider == 'geoserver') {
		this.geoPolygonLayerRef.removePolygon(a);
	} else
		return a.polygon.setMap(null);
};

GPS_Map.prototype.addControl = function(a) {
	// console.log(a);
	// return this.map.addControl(a.control);
};
GPS_Map.prototype.clearOverlays = function() {
	if (this.mapProvider == 'geoserver') {
		var removes = [];
		for ( var i = 0; i < this.geoMarkers.markers.length; i++) {
			var m = this.geoMarkers.markers[i];
			removes.push(m);
		}
		for ( var i = 0; i < removes.length; i++) {
			var m = removes[i];
			this.geoMarkers.removeMarker(m);
		}
		this.geoLineLayerRef.removeAllLines();
	} else {
		if (this.googleMarkers) {
			for ( var i = 0; i < this.googleMarkers.length; i++) {
				this.googleMarkers[i].setMap(null);
			}
		}
		if (this.googleLines) {
			for ( var i = 0; i < this.googleLines.length; i++) {
				this.googleLines[i].setMap(null);
			}
		}

	}
};
GPS_Map.prototype.getZoom = function() {
	return this.map.getZoom();
};
GPS_Map.prototype.getBounds = function() {
	return new GPS_Bounds(this.map.getBounds());
};
var GPS_Bounds = function(a) {
	this.bounds = a;
};
GPS_Bounds.prototype.getSouthWest = function() {
	return new GPS_LatLng(this.bounds.getSouthWest().lat(), this.bounds
			.getSouthWest().lng());
};
GPS_Bounds.prototype.getNorthEast = function() {
	return new GPS_LatLng(this.bounds.getNorthEast().lat(), this.bounds
			.getNorthEast().lng());
};
var GPS_MarkerOptions = function() {
	this.icon = null;
	this.draggable = false;
};
var GPS_Marker = function(a, b /* GPS_MarkerOptions */, map) {
	this.uin = null;
	this.lng = a.lng();
	this.lat = a.lat();
	// console.log("fdfdfdf",a.lng(), a.lat());
	var d = {
		draggable : false
	};
	if (b) {
		if (b.icon) {
			// c.iconSize = new GSize(b.icon.iconSize.width,
			// b.icon.iconSize.height);
			// c.iconAnchor = new GPoint(b.icon.iconAnchor.x,
			// b.icon.iconAnchor.y);
			// c.shadow = "";
			// c.image = b.icon.image;
			this.image = b.icon.image;
			this.width = b.icon.iconSize.width;
			this.height = b.icon.iconSize.height;
			if (b.icon.iconAnchor)
				this.iconAnchor = new google.maps.Point(b.icon.iconAnchor.x,
						b.icon.iconAnchor.y);
			var c = new google.maps.MarkerImage(b.icon.image,
					new google.maps.Size(b.icon.iconSize.width,
							b.icon.iconSize.height), null, this.iconAnchor);
			d.icon = c;
		}
		if (b.draggable) {
			d.draggable = b.draggable;
		}
		if (b.title) {
			d.title = b.title;
			this.title = b.title;
		}
		if (b.uin) {
			this.uin = b.uin;
		}
	}
	this.map = map;
	this.mapProvider = currentMap.mapProvider;
	this.gpsMap = currentMap; // GPS_Map
	if (currentMap.mapProvider == 'geoserver') {
		return;
	}
	d.position = new google.maps.LatLng(this.lat, this.lng);
	if (this.map)
		d.map = this.map;
	this.name = 'gps_marker_' + Math.random();
	this.marker = new google.maps.Marker(d);
};
GPS_Marker.prototype.setPoint = function(a) {
	return this.marker.setPoint(new GLatLng(a.lat(), a.lng()));
};
GPS_Marker.prototype.addListener = function(b, c) {
	var d = this;
	if (this.geoMarker) {
		var handler = {};
		handler[b] = function(a) {
			var point = a.object.lonlat;
			c(d, new GPS_LatLng(point.lat, point.lon));
		};
		this.geoMarker.events.on(handler);
		return;
	}

	return google.maps.event.addListener(this.marker, b, function(a) {
		if (a && a.latLng) {
			var pos = new GPS_LatLng(a.latLng.lat(), a.latLng.lng());
			c(d, pos);
		} else {
			c(d, a);
		}
	});
};

GPS_Marker.prototype.removeAllListener = function() {
	google.maps.event.clearListeners(this.marker);
};

GPS_Marker.prototype.openInfoWindowHtml = function(a, op) {
	var self = this;
	if (this.geoMarker) {
		if (this.popup)
			this.popup.destroy();

		var popup = new OpenLayers.Popup("chicken", new OpenLayers.LonLat(
				op.lng, op.lat), new OpenLayers.Size(180, 120), a, true,
				function(e) {
					if (self.popup) {
						self.popup.hide();
					}
					OpenLayers.Event.stop(e);
					op.onCloseFn();
				});
		// popup.setBackgroundColor("red");
		// popup.setOpacity(.5);
		popup.setBorder("1px solid"); // onCloseFn

		this.map.addPopup(popup);
		this.popup = popup;
	} else {
		if (this.infowindow) {
			google.maps.event.clearListeners(this.infowindow, 'closeclick');
			google.maps.event.addListener(this.infowindow, 'closeclick',
					op.onCloseFn);
			this.infowindow.setContent(a);
			return this.infowindow.open(this.map, this.marker);
		}
		var infowindow = new google.maps.InfoWindow({
			content : a
		});
		this.infowindow = infowindow;
		google.maps.event.clearListeners(this.infowindow, 'closeclick');
		google.maps.event.addListener(this.infowindow, 'closeclick',
				op.onCloseFn);
		return infowindow.open(this.map, this.marker);
	}
};

GPS_Marker.prototype.closeInfoWindow = function() {
	var self = this;
	if (this.geoMarker) {
		if (this.popup)
			this.popup.destroy();
	} else {
		if (this.infowindow) {
			return this.infowindow.close();
		}
	}
};

GPS_Marker.prototype.remove = function() {
	if (this.gpsMap.mapProvider == 'geoserver') {
		this.gpsMap.geoMarkers.removeMarker(this.geoMarker);
	} else
		return this.marker.setMap(null);
};

GPS_Marker.prototype.getPoint = function() {
	return new GPS_LatLng(this.marker.getPosition().lat(), this.marker
			.getPosition().lng());
};
GPS_Marker.prototype.setPoint = function(a /* GPS_LatLng */) {
	this.lat = a.lat();
	this.lng = a.lng();
	if (this.geoMarker) {
		var lonlat = new OpenLayers.LonLat(a.lng(), a.lat());
		var px = this.geoMarker.map.getLayerPxFromLonLat(lonlat);
		// console.log(a, this.geoMarker, px);
		return this.geoMarker.moveTo(px);
	} else
		return this.marker
				.setPosition(new google.maps.LatLng(a.lat(), a.lng()));
};
GPS_Marker.prototype.setImage = function(a, iconSize, iconAnchor) {
	if (this.geoMarker) {
		this.geoMarker.setUrl(a);
	} else {
		var c = new google.maps.MarkerImage(a, iconSize ? new google.maps.Size(
				iconSize.width, iconSize.height) : null, null,
				iconAnchor ? new google.maps.Point(iconAnchor.x, iconAnchor.y)
						: null);
		return this.marker.setIcon(c);
	}
};
GPS_Marker.prototype.hide = function() {
	if (this.geoMarker) {
		this.geoMarker.display(false);
	} else
		return this.marker.setVisible(false);
};
GPS_Marker.prototype.show = function() {
	if (this.geoMarker) {
		this.geoMarker.display(true);
	} else
		return this.marker.setVisible(true);
};
GPS_Marker.prototype.alert = function(a, b) {
	this.marker.openInfoWindowHtml(a);
	google.maps.event.addListener(this.marker, "infowindowclose", b);
};
var GPS_Polyline = function(a, b, c, name1, clickable) {
	this.length = 0;
	var d = Array();
	for ( var i = 0; i < a.length; i++) {
		if (i > 0) {
			this.length += a[i].distanceFrom(a[i - 1]);
		}
		d.push(new google.maps.LatLng(a[i].lat(), a[i].lng()));
	}
	var config = {
		path : d,
		strokeColor : b,
		strokeWeight : c,
		clickable : clickable ? true : false
	};
	this.polyline = new google.maps.Polyline(config);
	this.geoLine = {
		name : name1,
		points : a
	};
};
GPS_Polyline.prototype.getLength = function() {
	return this.length;
};

var GPS_Polygon = function(points, strokeColor, strokeWeight, fillColor,
		fillOpacity, clickable) {
	var d = [];
	for ( var i = 0; i < points.length; i++) {
		d.push(new google.maps.LatLng(points[i].lat(), points[i].lng()));
	}
	var config = {
		paths : d,
		strokeColor : strokeColor,
		strokeWeight : strokeWeight
	};
	if (fillColor) {
		config.fillColor = fillColor;
		config.fillOpacity = 0.5;
	}
	if (fillOpacity) {
		config.fillOpacity = fillOpacity;
	}
	if (clickable === false || clickable === true) {
		config.clickable = clickable;
	}
	this.config = config;
	this.points = points;
	this.polygon = new google.maps.Polygon(config);
	this.name = "geoPolygon_" + Math.random();
};

var GPS_Geocoder = function() {
	this.geocoder = new google.maps.Geocoder();
};
GPS_Geocoder.prototype.getLocations = function(e, f) {
	return this.geocoder.geocode({
		location : new google.maps.LatLng(e.lat(), e.lng()),
		language : locale
	}, function(results, status) {
		try {
			if (google.maps.GeocoderStatus.OK == status) {
				// var address = results[0].address_components[0].long_name;
				var address = results[0].formatted_address;
				// console.log(results);
				f(e, address);
			}
		} catch (e1) {
			if (typeof console != "undefined")
				console.log("e:", e.lat(), e.lng(), e1);
		}
	});
};
function GPS_DownloadUrl(c, d) {
	GDownloadUrl(c, function(a, b) {
		d(a, b);
	});
};

